//
//  MineViewController.m
//  foodlist
//
//  Created by HappyMilk 02 on 2017/8/15.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "MineViewController.h"
#import "MJUserModel.h"
#import "LoginHeader.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import "MJMyMsgController.h"
#import "LoginViewController.h"
#import "CollectViewController.h"
#import "AboutViewController.h"

@interface MineViewController ()

@property (weak, nonatomic) IBOutlet UIButton *btn_head;
@property (weak, nonatomic) IBOutlet UILabel *lab_username;

@property (nonatomic ,strong) MJUserModel *userModel;


@end

@implementation MineViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [MJUserModel registerSubclass];
    if ([MPWeChat stander].m_isLogin) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"first"]) {
            
            [self findUserData];
        }else{
            
            [self createUserModel];
        }
    }
}
- (void)findUserData{
    
    AVQuery *query = [AVQuery queryWithClassName:@"User_list"];
    NSString *objcid = [[NSUserDefaults standardUserDefaults] objectForKey:@"first"];
    [query getObjectInBackgroundWithId:objcid block:^(AVObject * _Nullable object, NSError * _Nullable error) {
        MJUserModel *model = (MJUserModel *)object;
        _userModel = model;
        self.lab_username.text = model.name;
        [self.btn_head sd_setImageWithURL:[NSURL URLWithString:model.head_img.url] forState:UIControlStateNormal];
    }];
}
- (void)createUserModel{
    
    MJUserModel *model = [MJUserModel object];
    NSLog(@"------ %@",model.objectId);
    model.user_id = @"";
    model.name = @"李某某";
    model.phoneNum = @"暂无";
    model.sex = @"男";
    model.adrs = @"暂无";
    model.adrs_now = @"暂无";
    model.birthday = @"暂无";
    model.height = @"暂无";
    model.school_record = @"暂无";
    model.enmotion = @"暂无";
    model.signature = @"暂无";
    UIImage *image = [UIImage imageNamed:@"min_userhead"];
    NSData *imageData=UIImageJPEGRepresentation(image,0.6);
    AVFile *file = [AVFile fileWithName:@"more_head_df.png" data:imageData];
    model.head_img = file;
    [model saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
        [[NSUserDefaults standardUserDefaults] setObject:model.objectId forKey:@"first"];
        
    }];
    _userModel = model;
    self.lab_username.text = model.name;
    [self.btn_head sd_setImageWithURL:[NSURL URLWithString:model.head_img.url] forState:UIControlStateNormal];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.btn_head.layer.masksToBounds = YES;
    self.btn_head.layer.cornerRadius = self.btn_head.frame.size.width/2;
}

- (IBAction)userheadAction:(UIButton *)sender {
    if ([MPWeChat stander].m_isLogin) {
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"mine" bundle:nil];
        MJMyMsgController *vc = [sb instantiateViewControllerWithIdentifier:@"MJMyMsgController"];
        vc.userModel = _userModel;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"login" bundle:nil];
        LoginViewController *vc = [sb instantiateViewControllerWithIdentifier:@"LoginViewController"];
        [self presentViewController:vc animated:YES completion:^{
            
        }];
    }
}
- (IBAction)myCollectAction:(UIButton *)sender {
    
    if ([[MyAdTools shareInstance] showInterstitialWithVC:self]) {
        return;
    }
    
    VC_INIT_WITH_STORYBOARD(Main, CollectViewController, vc)
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)feedback:(id)sender {
    [[UIPasteboard generalPasteboard] setString:@"759120403@qq.com"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:759120403@qq.com"]];
}
- (IBAction)about:(id)sender {
    
    VC_INIT_WITH_STORYBOARD(Main, AboutViewController, vc)
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)setAction:(id)sender {
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
